import Vue from 'vue'

import Cookies from 'js-cookie'

import 'normalize.css/normalize.css' // a modern alternative to CSS resets

import Element from 'element-ui'

import App from './App'
import store from './store'

import './styles/index.scss'

import './assets/iconfont.css'

if (process.env.NODE_ENV === 'production') {
  require('./styles/output/element.css')
  require('./styles/output/index.css')
} else {
  require('@neu/biz-core/styles/theme-default/element.scss')
  require('@neu/biz-core/styles/theme-default/index.scss')
}

import router, { registerRouter } from './router'
// import authRouter from './router/authrouters'

import './icons' // icon
import './permission' // permission control
import './utils/error-log' // error log

import * as filters from './filters' // global filters
import { getTheme, toggleClass, isArray } from './utils/utils'

import Treeselect from './components/TreeSelect'
// import Treeselect from '@riophae/vue-treeselect'
// import '@riophae/vue-treeselect/dist/vue-treeselect.css'
Vue.component('treeselect', Treeselect)

import { TableColumnRender } from './utils/components'
Vue.component('columnRender', TableColumnRender)

import echarts from 'echarts'
import 'echarts-gl'
Vue.prototype.$echarts = echarts
/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online ! ! !
 */
// if (process.env.NODE_ENV === 'production') {
//   const { mockXHR } = require('../mock')
//   mockXHR()
// }

Vue.use(Element, {
  size: Cookies.get('size') || 'medium' // set element-ui default size
})

// register global utility filters
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})

Vue.config.productionTip = false
Vue.config.optionMergeStrategies.created = function(parentVal, childVal) {
  return childVal === undefined
    ? parentVal
    : [childVal]
}

export function register(globalOptions) {
  const opts = Object.assign(
    {
      el: '#app',
      currentAppKey: '__currentApp',
      topMenus: []
    },
    globalOptions
  )

  store.dispatch('app/setTopMenus', opts.topMenus)

  // 初始化路由
  registerRouter(opts.router)
  if (opts.store && opts.store.length > 0) {
    opts.store.forEach(o => {
      store.registerModule(o.moduleName, o.store)
      if (o.autoRun) {
        const autoRun = isArray(o.autoRun) ? o.autoRun : [o.autoRun]
        store.dispatch('app/setAutoRun', autoRun)
      }
    })
  }

  if (opts.siteTitle) {
    store.dispatch('app/setTitle', opts.siteTitle)
  }

  if (opts.hasOwnProperty('wsEnabled')) {
    store.dispatch('app/setWsEnabled', !!opts.wsEnabled)
  }

  window[opts.currentAppKey] = new Vue({
    router,
    store,
    render: h => h(App)
  }).$mount(opts.el)

  toggleClass(document.body, getTheme())
}

// register({
// })
